package com.vstudy.zx.controller;

import com.vstudy.zx.entity.Article;
import com.vstudy.zx.entity.Grade;
import com.vstudy.zx.entity.Subject;
import com.vstudy.zx.entity.Teacher;
import com.vstudy.zx.service.ArticleManageService;
import com.vstudy.zx.utils.IOUtil;
import com.vstudy.zx.utils.page.ArticlePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/articleManage")
public class ArticleManagerController {
    @Autowired
    private ArticleManageService articleManageService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ArticlePage page, Model model, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        List<Article> articles = articleManageService.getArticlePage(page, teacher.getTe_id(), page.getGrade_id(), page.getSubject_id());
        model.addAttribute("articles", articles);
        // 选项
        List<Grade> grades = articleManageService.getGrades();
        model.addAttribute("grades", grades);
        List<Subject> subjects = articleManageService.getSubjects();
        model.addAttribute("subjects", subjects);
        model.addAttribute("page", page);
        return "zx/articleManage/index.html";
    }


    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, Model model, HttpServletRequest request) {
        Article article = articleManageService.getArticleById(id);
        if (article == null) {
            return "error.jsp";
        }
        String realPath = request.getServletContext().getRealPath(article.getArticle_content());
        try {
            String html = IOUtil.readArticle(realPath, "utf-8");
            article.setArticle_content(html);
            model.addAttribute("article", article);
            return "/articleManage/edit.jsp";
        } catch (IOException e) {
            e.printStackTrace();
            return "zx/error.jsp";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(String articleHtml, String title, Long articleId, HttpServletRequest request) {
        boolean rs = articleManageService.updateArticle(articleHtml, title, articleId, request);
        if (rs) {
            return 1;
        }
        return 0;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public Object remove(Long id, HttpServletRequest request) {
        int rs = articleManageService.removeArticle(id, request);
        if (rs > 0) {
            return 1;
        }
        return 0;
    }
}
